package com.example.ranl_safeapi.AOP;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

@Aspect
@Component
@EnableScheduling
public class RequestCountAspect {

    private final AtomicInteger requestCount = new AtomicInteger(0);
    private volatile int requestCountPerSecond = 0;

    @Autowired
    private ApiTrafficMonitorAspect apiTrafficMonitorAspect; // ApiTrafficMonitorAspect的组件用于获取请求量和响应时间

    @Before("@annotation(org.springframework.web.bind.annotation.GetMapping) && execution(* com.example.ranl_safeapi.Controller.StudentController.getUserList(..))")
    public void countRequest() {
        requestCount.incrementAndGet();
    }

    @Scheduled(fixedRate = 1000)
    public void printRequestCountPerSecond() {
        int count = requestCount.getAndSet(0);
        System.out.println("Requests per second: " + count);
    }
}
